package edu.gdkm.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.gdkm.controller.request.BaseRequest;
import edu.gdkm.entity.User;
import edu.gdkm.mapper.UserMapper;
import edu.gdkm.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class UserService implements IUserService {
    @Autowired
   private UserMapper userMapper;

    /**
     * 查询全部
     * @return
     */
    @Override
    public List<User> list() {
        return userMapper.list();
    }

    /**
     * 分页查询 模糊查询 PageHelper插件
     * @param userPageRequest
     * @return
     */
    @Override
    public PageInfo<User> page(BaseRequest baseRequest) {
        PageHelper.startPage(baseRequest.getPageNum(),baseRequest.getPageSize());
        List<User> users = userMapper.listByCondition(baseRequest);
        return new PageInfo<>(users);
    }

    /**
     * 新增用户
     */
    @Override
    public void save(User user) {
        Date date= new Date();
        //设置username唯一 日期 + UUID TODO 雪花Id
        user.setUsername(DateUtil.format(date,"yyyyMMdd") + Math.abs(IdUtil.fastSimpleUUID().hashCode()));
        userMapper.save(user);
    }


    /**
     * 通过id获取用户数据(修改用户)
     * @return
     */
    @Override
    public User getById(Integer id) {
        return userMapper.getById(id);

    }

    /**
     * 修改信息
     * @param user
     * @return
     */
    @Override
    public void update(User user) {
        user.setUpdatetime(new Date());
         userMapper.updateById(user);

    }

    @Override
    public void deleteById(Integer id) {
        userMapper.deleteById(id);
    }
}
